cmake_minimum_required(VERSION 3.10)
project(MessageFramework VERSION 1.0.0)

# C++17标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 编译选项
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
    add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# 查找依赖
find_package(OpenSSL REQUIRED)

# 使用顶层统一配置的spdlog
if(NOT TARGET spdlog_unified)
    message(FATAL_ERROR "spdlog_unified not found!")
endif()

# 头文件目录
include_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}/include
    ${CMAKE_CURRENT_SOURCE_DIR}/../include
    ${CMAKE_CURRENT_SOURCE_DIR}/../TraderManager/include  # 访问RedisPool.h
    # spdlog include dirs are provided by spdlog_unified target
)

# 源文件
set(FRAMEWORK_SOURCES
    src/MessageHeader.cpp
    src/SignatureHelper.cpp
    src/EncryptionHelper.cpp
    src/MessageDispatcher.cpp
    src/MessageProducer.cpp
    src/MessageConsumer.cpp
)

# Handler源文件
set(HANDLER_SOURCES
    handlers/TradeSignalHandler.cpp
    handlers/APIRequestHandler.cpp
)

# 构建MessageFramework静态库
add_library(MessageFramework STATIC
    ${FRAMEWORK_SOURCES}
    ${HANDLER_SOURCES}
)

# 链接依赖
target_link_libraries(MessageFramework
    PUBLIC
        spdlog_unified
    PRIVATE
        OpenSSL::SSL
        OpenSSL::Crypto
        pthread
)

# 安装规则
install(TARGETS MessageFramework
    ARCHIVE DESTINATION lib
    LIBRARY DESTINATION lib
)

install(DIRECTORY include/
    DESTINATION include/MessageFramework
    FILES_MATCHING PATTERN "*.h"
)

install(DIRECTORY handlers/
    DESTINATION include/MessageFramework/handlers
    FILES_MATCHING PATTERN "*.h"
)

# 使用示例（可选编译）
option(BUILD_EXAMPLES "Build MessageFramework examples" OFF)

if(BUILD_EXAMPLES)
    add_subdirectory(examples)
endif()

# 单元测试（可选编译）
option(BUILD_TESTS "Build MessageFramework tests" OFF)

if(BUILD_TESTS)
    enable_testing()
    add_subdirectory(tests)
endif()
